package com.example.su_shi_tan_mine.child

import android.view.View
import android.widget.EditText
import android.widget.ImageView
import androidx.appcompat.widget.Toolbar
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.fastjson.JSON
import com.effective.android.panel.PanelSwitchHelper
import com.example.library_common.utils.normalShortToast
import com.example.su_shi_tan_bas_lib.base.BaseFragmentEx
import com.example.su_shi_tan_bas_lib.base.BaseLazyDataBindingFragment
import com.example.su_shi_tan_bas_lib.base.LazyFragment
import com.example.su_shi_tan_bas_lib.networking.RestClient
import com.example.su_shi_tan_core.arouter.MineARouter
import com.example.su_shi_tan_mine.R
import com.example.su_shi_tan_mine.child.provider.UnfilledHeight
import com.example.su_shi_tan_mine.databinding.ServiceCustomerFragmentBinding

@Route(path = MineARouter.SERVICE_CUSTOMER)
class ServiceCustomerFragment : BaseLazyDataBindingFragment<ServiceCustomerFragmentBinding>(),
    View.OnClickListener {

    private var mListFragment = ServiceCustomerListFragment.newInstance(
        object : UnfilledHeight {
            override fun setUnfilledHeight(newUnfilledHeight: Int) {
                unfilledHeight = newUnfilledHeight
            }
        }
    )

    private var mHelper: PanelSwitchHelper? = null

    private var unfilledHeight = 0

    private val msgEdt by lazy {
        view?.findViewById<EditText>(R.id.msgEdt)
    }

    private val sendBtn by lazy {
        view?.findViewById<ImageView>(R.id.sendBtn)
    }

    private val mToolbar by lazy {
        view?.findViewById<Toolbar>(R.id.mToolbar)
    }

    override fun layoutId(): Int {
        return R.layout.service_customer_fragment
    }


    override fun initView(view: View) {
        mToolbar?.title = "客服"
        mToolbar?.setNavigationOnClickListener { pop() }
        sendBtn?.setOnClickListener(this)
    }

    override fun lazyInit() {
        if (mHelper == null) {
            mHelper = PanelSwitchHelper.Builder(this)
                .contentScrollOutsideEnable(true)

                .addContentScrollMeasurer {
                    getScrollDistance {
                        it - unfilledHeight
                    }

                    getScrollViewId { R.id.list_panel }
                }
                .build()
        }
        loadRootFragment(R.id.list_panel, mListFragment)
    }


    override fun onBackPressedSupport(): Boolean {
        if (mHelper?.hookSystemBackByPanelSwitcher() == true) {
            return true
        }

        pop()

        return true
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.sendBtn -> {
                val msg = msgEdt?.text.toString()
                sendMsg(msg)
                hideSoftInput()
            }
        }

    }

    private fun sendMsg(content: String) {
        if (content.isEmpty()) return
        RestClient.builder()
            .url(R.string.api_converse)
            .raw(
                JSON.toJSONString(
                    mapOf(
                        "content" to content
                    )
                )
            ).success {
                mListFragment.refreshData()
            }.error { _, msg ->
                normalShortToast(_mActivity, msg)
            }.failure {
                normalShortToast(_mActivity, R.string.net_work_failed)
            }.build().post()
    }


    override fun hideSoftInput() {
        super.hideSoftInput()
        msgEdt?.setText("")
    }


}